using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.GamerServices;


namespace ebookreader
{
    public class BookMark
    {
        public int word;
        public string name;
    }

    public class Book
    {
        public string title="";
        public string authors="";
        public string lang="";
        public DateTime lastRead;
        public int currentWord;
        public int total_pages;
        public String filename;
        public List<BookMark> bookmarksList = new List<BookMark>();
    }


    public class State
    {
        public int numDownloaded;
        public List<Book> booksList { get; set; }
        public Book currentBook { get; set; }
        public optionsData options;
        public int selected_language;
        public bool[] hints;

        public State()
        {
            booksList = new List<Book>();
            options = new optionsData();
            hints = new bool[10];
        }

        private void Initialize()
        {
        }


        public static State loadSate()
        {
            return RestoreState();
        }


        #region Save & Restore State

        private static State RestoreState()
        {
#if WINDOWS_PHONE
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream;
            try
            {
                stream = store.OpenFile("state.txt", FileMode.Open, FileAccess.ReadWrite);
            }
            catch (Exception)
            {
                store.Dispose();
                return new State();
            }
#else
			FileStream stream = File.Open(FileName, FileMode.Create);			
#endif

            XmlSerializer serializer = new XmlSerializer(typeof(State));            
            var state = serializer.Deserialize(stream) as State;
            
            stream.Close();

#if WINDOWS_PHONE
            store.Dispose();
#endif
            return state;
        }

        public void SaveState()
        {
#if WINDOWS_PHONE
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream = store.OpenFile("state.txt", FileMode.Create, FileAccess.ReadWrite);
#else
			FileStream stream = File.Open(FileName, FileMode.Create);			
#endif

            XmlSerializer serializer = new XmlSerializer(typeof(State));
            serializer.Serialize(stream, this);

            stream.Close();

#if WINDOWS_PHONE
            store.Dispose();
#endif
        }

        #endregion


    }
}
